package com.easy.dn.ui.usercenter.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.easy.common.base.BaseActivity;
import com.easy.common.baserx.RxBus;
import com.easy.common.baserx.RxSubscriber;
import com.easy.common.commonwidget.DnToolbar;
import com.easy.dn.api.Api;
import com.easy.dn.api.ApiService;
import com.easy.dn.app.AppConstant;
import com.easy.dn.bean.InfoRefreshBean;
import com.easy.dn.bean.UserDataBean;
import com.easy.dn.utils.MapHelper;
import com.ta.utdid2.android.utils.StringUtils;

import java.util.Map;

import butterknife.Bind;
import dn.easy.com.easyto.R;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 公司名称
 */
public class CompanyNameActivity extends BaseActivity implements DnToolbar.OnRightTitleClickListener {

    @Bind(R.id.myInfoCompanyName)
    EditText mCompanyName;
    @Bind(R.id.et_website)
    EditText mWebsite;
    @Bind(R.id.editone)
    EditText mEditDescription;
    private int mFlag;

    @Override
    public int getLayoutId() {
        return R.layout.act_company_name;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
        initTitle();
        mToolbar.setMainTitle(R.string.company_name);
        mToolbar.setMainTitleRightText(R.string.save);
        mToolbar.setOnRightTitleClickListener(this);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            mFlag = bundle.getInt(AppConstant.INFOTITLE);
            String name = bundle.getString(AppConstant.INFODATA);
            String website = bundle.getString(AppConstant.INFOCOMPANYUIL);
            String description = bundle.getString(AppConstant.INFOCOMPANYDES);
            mCompanyName.setText(name);
            mWebsite.setText(website);
            mEditDescription.setText(description);
        }else {
            mCompanyName.setText("");
            mWebsite.setText("");
            mEditDescription.setText("");
        }
    }

    @Override
    public void onRightClick(View view) {
        if (!StringUtils.isEmpty(mCompanyName.getText().toString().trim())) {
            Map<String, String> map = MapHelper.getAccessMap();
            map.put("companyName", mCompanyName.getText().toString());
            map.put("companyUrl", mWebsite.getText().toString() == null ? "" : mWebsite.getText().toString());
            map.put("companyDescription", mEditDescription.getText().toString() == null ? "" : mEditDescription.getText().toString());
            Api.getInstance().getProxy(ApiService.class).updateUserInfoData(Api.getCacheControl(), map).
                    subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).
                    subscribe(new RxSubscriber<UserDataBean>(this, true) {
                        @Override
                        protected void _onNext(UserDataBean info) {
                            InfoRefreshBean bean = new InfoRefreshBean();
                            bean.setFlag(3);
                            bean.setContent(mCompanyName.getText().toString().trim());
                            RxBus.getInstance().post(AppConstant.INFOREFRESH, bean);
                            finish();
                        }

                        @Override
                        protected void _onError(String message) {
                            showShortToast(message);
                        }
                    });
        } else {
            showShortToast(R.string.input_company_name);
        }
    }

}
